package com.ll.app.controller;

import com.ll.app.api.req.AddCourseReqDTO;
import com.ll.app.api.req.DeleteCourseReqDTO;
import com.ll.app.api.req.PageCourseReqDTO;
import com.ll.base.dto.ApiResultDTO;
import com.ll.domain.classbase.biz.CourseDomainService;
import com.ll.domain.classbase.enums.CourseTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;

import static java.util.concurrent.Executors.newCachedThreadPool;

@Slf4j
@RestController
@RequestMapping("/course")
@RequiredArgsConstructor
@Api(tags = "训练课程API")
public class CourseController {

    private final CourseDomainService courseDomainService;

    @ResponseBody
    @ApiOperation(value = "分页查看")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ApiResultDTO page(@RequestBody @Validated PageCourseReqDTO pageReq) {
        return ApiResultDTO.success(courseDomainService.page(pageReq));
    }

    @ResponseBody
    @ApiOperation(value = "添加训练课程")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResultDTO add(@RequestBody AddCourseReqDTO courseDTO) {
        courseDomainService.add(courseDTO);
        return ApiResultDTO.success();
    }

    @ResponseBody
    @ApiOperation(value = "删除训练课程")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ApiResultDTO delete(@RequestBody DeleteCourseReqDTO reqDTO) {
        courseDomainService.delete(reqDTO);
        return ApiResultDTO.success();
    }


    @ResponseBody
    @ApiOperation(value = "测试批量添加课程")
    @RequestMapping(value = "/init", method = RequestMethod.POST)
    public ApiResultDTO delete(@RequestParam("size") Long size) {
        // 创建线程池（使用了预定义的配置）
        ExecutorService executor = newCachedThreadPool();
        Date date = new Date();
        for (long i = 0; i < 10; i++) {
            long finalI = i;
            executor.execute(() -> {
                for(int j = 0; j < size/10; j ++){
                    AddCourseReqDTO reqDTO = AddCourseReqDTO.builder()
                            .dingTalkUserId("TestUser:" + finalI + "_" +j)
                            .endTime(date)
                            .maxPeople(10)
                            .startTime(date)
                            .teacher("TestTeacher" + finalI+ "_" +j)
                            .title("TestTitle" + finalI+ "_" +j)
                            .type(CourseTypeEnum.公开训练)
                            .build();
                    courseDomainService.add(reqDTO);
                }

            });
        }
        return ApiResultDTO.success();
    }


    // 异步任务
    static class Task implements Callable<Integer> {
        // 返回异步任务的执行结果
        @Override
        public Integer call() throws Exception {
            int i = 0;
            for (; i < 10; i++) {
                try {
                    System.out.println(Thread.currentThread().getName() + "_"
                            + i);
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return i;
        }
    }
}


